package com.tanhua.server.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.domain.db.Announcement;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.Comment;
import com.tanhua.domain.vo.AnnouncementVo;
import com.tanhua.domain.vo.MessageLikeVo;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.dubbo.api.MessagesApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Service
public class MessagesService {

    @Reference
    private MessagesApi messagesApi;
    @Reference
    private UserInfoApi userInfoApi;

    public ResponseEntity<Object> findAll(Integer page, Integer pageSize) {

        IPage<Announcement> iPage = messagesApi.findAll(page, pageSize);
        List<Announcement> announcements = iPage.getRecords();

        // 封装数据返回
        List<AnnouncementVo> voList = new ArrayList<>();
        for (Announcement announcement : announcements) {
            AnnouncementVo vo = new AnnouncementVo();
            BeanUtils.copyProperties(announcement, vo);
            // 封装日期
            vo.setCreateDate(new SimpleDateFormat("yyyy-MM-dd").format(announcement.getCreated()));

            voList.add(vo);
        }

        // 构造分页对象返回
        PageResult pageResult = new PageResult(page, pageSize, (int) iPage.getTotal(), voList);

        return ResponseEntity.ok(pageResult);
    }

    /**
     * 接口名称：点赞列表
     *
     * @return
     */
    public ResponseEntity<Object> likes(Integer commentType, Integer page, Integer pagesize) {

        // 获取用户id
        Long publishUserId = UserHolder.getUserId();

        // 调用api
        PageResult pageResult = messagesApi.likes(publishUserId, commentType, page, pagesize);
        // 获取数据
        List<Comment> comments = (List<Comment>) pageResult.getItems();
        // 创建集合封装数据
        List<MessageLikeVo> voList = new ArrayList<>();

        if (!CollectionUtils.isEmpty(comments)) {
            for (Comment comment : comments) {
                MessageLikeVo vo = new MessageLikeVo();
                // 获取用户详细信息
                Long userId = comment.getUserId();
                UserInfo userInfo = userInfoApi.findById(userId);
                if (userInfo != null) {
                    vo.setId(userInfo.getId().toString());
                    vo.setAvatar(userInfo.getAvatar());
                    vo.setNickname(userInfo.getNickname());
                }
                vo.setCreateDate(new SimpleDateFormat("yyyy-MM-dd").format(comment.getCreated()));
                // 加入集合
                voList.add(vo);
            }
        }

        pageResult.setItems(voList);
        return ResponseEntity.ok(pageResult);
    }
}
